package com.corticon.dialogs.util;

import java.io.Serializable;

/**
 * Purpose: Pagination.
 * 
 * @author Yanglifan
 */
public class Pager implements Serializable {

    private long numOfRecords = 0l;
    private int page = 1;
    private int pageSize = 10;
    private String sortColumn;
    private boolean sortAsc;
    private String rootUrl;
    private String requestParameters = "";
    // ********** Constructors ********** //
    public Pager() {
    }

    public Pager(int pageSize) {
        this.pageSize = pageSize;
    }

    public Pager(int page, int pageSize) {
        this.page = page;
        this.pageSize = pageSize;
    }

    public int getNextPage() {
        return page + 1;
    }

    public int getPreviousPage() {
        return page - 1;
    }

    public int getFirstPage() {
        return 0;
    }

    /**
     * The number start from 1.
     * @return
     */
    public int getFirstRecord() {
        return (page - 1) * pageSize;
    }

    public long getLastRecord() {
        return ((page - 1) * pageSize + pageSize) > numOfRecords
                ? numOfRecords
                : (page - 1) * pageSize + pageSize;
    }

    public long getLastPage() {
        long lastPage = (numOfRecords / pageSize);
        if (numOfRecords % pageSize == 0) {
            lastPage--;
        }
        return lastPage;
    }

    public boolean isNextPageAvailable() {
        return numOfRecords > (((page - 1) * pageSize) + pageSize);
    }

    public boolean isPreviousPageAvailable() {
        return page - 1 > 0;
    }

    // ********** Getters and Setters ********** //
    public long getNumOfRecords() {
        return numOfRecords;
    }

    public void setNumOfRecords(long numOfRecords) {
        this.numOfRecords = numOfRecords;
    }

    public int getPage() {
        return page;
    }

    public void setPage(int page) {
        this.page = page;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public String getSortColumn() {
        return sortColumn;
    }

    public void setSortColumn(String sortColumn) {
        this.sortColumn = sortColumn;
    }

    public boolean isSortAsc() {
        return sortAsc;
    }

    public void setSortAsc(boolean sortAsc) {
        this.sortAsc = sortAsc;
    }

    public String getRootUrl() {
        return rootUrl;
    }

    public void setRootUrl(String rootUrl) {
        this.rootUrl = rootUrl;
    }

    public String getRequestParameters() {
        return requestParameters;
    }

    public void setRequestParameters(String requestParameters) {
        this.requestParameters = requestParameters;
    }
}
